﻿@model GolfLeague.EditModels.TeamEditModel

<script type="text/javascript">
    $(document).ready(function () {
        $('#dialog').dialog({
            autoOpen: false,
            model: true
        });
        $('input:button').button();
        $('input:submit').button();
        $('input:reset').button();
        $('div#buttonlist a').hover(
		    function () { $(this).addClass('ui-state-hover'); },
		    function () { $(this).removeClass('ui-state-hover'); }
	    );
    });

    $(function () {
        $(".DeleteButton").click(function (e) {
            e.preventDefault();
            var recordToDelete = $(this).attr("id");
            recordToDelete = recordToDelete.replace("delete-", "");

            $("#dialog").dialog("option", "buttons", {
                "Yes": function () {
                    $(this).dialog("close");
                    if (recordToDelete != '') {
                        $.post("/Team/TeamDelete", { "id": recordToDelete },
                        function (data) {
                            $('#row-' + recordToDelete).fadeOut('slow');
                        });
                    }
                },
                "No": function () {
                    $(this).dialog("close");
                    return false;
                }
            });
            $("#dialog").dialog("open");
        });
    });

    $(function () {
        $(".EditButton").click(function (e) {
            e.preventDefault();
            var recordToEdit = $(this).attr("id");
            recordToEdit = recordToEdit.replace("edit-", "");

            if (recordToEdit != '') {
                window.location = '/Team/TeamEdit/' + recordToEdit;
            }
        });
    });

    $(function () {
        $(".ActivateButton").click(function (e) {
            var recordToActivate = $(this).attr("id");
            recordToActivate = recordToActivate.replace("activate-", "");

            if (recordToActivate != '') {
                window.location = "/Team/TeamActivate/" + recordToActivate;
            }
        });
    });

    $(function () {
        $(".DeactivateButton").click(function (e) {
            var recordToDeactivate = $(this).attr("id");
            recordToDeactivate = recordToDeactivate.replace("deactivate-", "");

            if (recordToDeactivate != '') {
                window.location = "/Team/TeamDeactivate/" + recordToDeactivate;
            }
        });
    });

    $(function () {
        $(".CancelEditButton").click(function (e) {
            e.preventDefault();
            window.location = "/Team/";
        });
    });

</script>

@{
    ViewBag.Title = "Teams";
}
@Html.ActionLink("Admin Home", "Admin", "Home") > Teams<br /><br />
@using (Html.BeginForm())
{
<fieldset>
    @if (Model.NewTeam.TeamID < 1)
    {
        <legend>Add Team</legend>
    }
    else
    {
        <legend>Edit Team</legend>
    }

    @Html.HiddenFor(model => model.NewTeam.TeamID)

    <div class="editor-label">
        @Html.LabelFor(model => model.NewTeam.TeamName)
    </div>
    <div class="editor-field" id="teamname">
        @Html.EditorFor(model => model.NewTeam.TeamName)
        @Html.ValidationMessageFor(model => model.NewTeam.TeamName)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.PlayerA.PlayerID)
    </div>
    <div class="editor-field" id="teamplayera">
        @Html.DropDownListFor(model => model.PlayerA.PlayerID, new SelectList(Model.Players, "dropdownID", "dropdownText"), Model.PlayerA.PlayerID)
        @Html.ValidationMessageFor(model => model.PlayerA.PlayerID)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.PlayerB.PlayerID)
    </div>
    <div class="editor-field" id="teamplayerb">
        @Html.DropDownListFor(model => model.PlayerB.PlayerID, new SelectList(Model.Players, "dropdownID", "dropdownText"), Model.PlayerB.PlayerID)
        @Html.ValidationMessageFor(model => model.PlayerB.PlayerID)
    </div>

    <p>
        @if (Model.NewTeam.TeamID < 1)
        {
            <input type="submit" value="Add" />
        }
        else
        {
            <input type="submit" value="Save" />
            <input type="reset" class="CancelEditButton" value="Cancel" />
        }
    </p>
    <div id="dialog" title="Confirm Delete">Are you sure you want to delete this team?</div>
</fieldset>
}

<fieldset>
    <legend>Teams</legend>
    <table>
        <tr>
            <th>
                Team Name
            </th>
            <th>
                Player A
            </th>
            <th>
                Player B
            </th>
            <th></th>
        </tr>

@foreach (var item in Model.Teams)
{
        <tr id="row-@item.TeamID" @if (item.EndedOn != null) {@Html.Raw("style=\"font-style:italic;background-color: Silver\"")}>
            <td>
                @item.TeamName
            </td>
            <td>
                @item.PlayerTeams.Where(t => t.TeamID == item.TeamID).First().Player.LastName, @item.PlayerTeams.Where(t => t.TeamID == item.TeamID).First().Player.FirstName
            </td>
            <td>
                @item.PlayerTeams.Where(t => t.TeamID == item.TeamID).Last().Player.LastName, @item.PlayerTeams.Where(t => t.TeamID == item.TeamID).Last().Player.FirstName
            </td>
            <td>
                <div id="buttonlist" style="vertical-align:middle">
                    <a href="#" title="Edit Team" class="EditButton ui-state-default ui-corner-all ui-icon ui-icon-pencil" id="edit-@item.TeamID"></a>
                    <a href="#" title="Delete Team" class="DeleteButton ui-state-default ui-corner-all ui-icon ui-icon-trash" id="delete-@item.TeamID"></a>
                </div>
                @if (item.EndedOn == null)
                {
                    <input type="button" class="DeactivateButton" id="deactivate-@item.TeamID" value="Mark Inactive" />
                }
                else
                {
                    <input type="button" class="ActivateButton" id="activate-@item.TeamID" value="Mark Active" />
                }
            </td>
        </tr>
}

    </table>
</fieldset>
@Html.ActionLink("Back to Admin Home", "Admin", "Home")